<?php
/**
 * The main template file.
 *
 * This is the most generic template file in a WordPress theme
 * and one of the two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * For example, it puts together the home page when no home.php file exists.
 *
 * Learn more: http://codex.wordpress.org/Template_Hierarchy
 *
 * @package WordPress
 * @subpackage Twenty_Twelve
 * @since Twenty Twelve 1.0
 */

require 'wp-blog-header.php';
require 'youku.php';
// require 'wp-content/themes/zhuli/functions.php';
?>


	<?php include 'wp-content/themes/zhuli/header.php'; ?>
	<link rel="stylesheet" type='text/css' href="<?php bloginfo('template_url'); ?>/css/zhuli-gallery.css">
	<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/zhuli-gallery.js"></script>

	<?php 
		
		$page_id = $_GET["g"]; 
		if ($_GET["p"] == '')
			$page_number = 1;
		else
			$page_number = intval($_GET["p"]);

		if ($page_id == "")
			$page_id = 'vision';
		
		function get_gallery_title() {
			global $page_id;
			switch ($page_id) {
				case 'vision':
					echo '印记大赏';
					break;
				case 'athena-base':
					echo '雅典娜教室基地';
					break;
				
				case 'coast-base':
					echo '环岛海岸基地';
					break;
				case 'universal-studio':
					echo '3D菲林环球影城';
					break;
				case 'gulangyu':
					echo '鼓浪屿';
					break;
				case 'xiamen':
					echo '厦门特色';
					break;
				case 'domestic-tours':
					echo '国内高端定制旅行婚纱';
					break;
				case 'foreign-tours':
					echo '国外高端定制旅行婚纱';
					break;
				case 'customer':
					echo '客照';
					break;
				case 'photo':
					echo '写真';
					break;
				case 'mv':
					echo 'MV';
					break;				
				case 'record':
					echo '婚庆纪实';
					break;
				
				default:
					echo '';
					break;
			}
		}

		?>
		<title><?php get_gallery_title(); ?>->主力摄影</title>
	</head>
	<body>
		<?php include 'wp-content/themes/zhuli/fixed-nav.php'; ?>

		<div id='p-container'>			
			<?php include 'wp-content/themes/zhuli/sidebar.php'; ?>

			<div id='p-gallery-content'>
				<h1><?php get_gallery_title(); ?></h1>
				<div class='p-gallery-photos'>
					<!--
					<?php if (class_exists('wp_media_tags_plugin')) 
						$elems = wp_media_tags_plugin::media_tags_query_array($page_id, 'medium', '-1', 'p-gallery-photo-thumb', 3); 
						foreach ($elems as $elem) {
							echo "<div class='p-gallery-photo-thumb-container'>" . $elem . "</div>";
						}

					?>-->
					<?php
						wp_reset_postdata();
							$posts_per_page = 9;
						// wp_reset_query(); 
						$args = array(
							'posts_per_page'	=>	$posts_per_page,
							'offset'			=>	($page_number-1) * $posts_per_page,
							'orderby'			=>	'post_date',
							'order'				=>	'DESC',
							'post_type'			=>	'post',
							'post_status'		=> 	'publish',
							'tag'				=>	$page_id );
						$wp_query = new WP_Query($args);
						if ( have_posts() ) :
						    while (have_posts()) : the_post();
								echo '<div class="p-gallery-photo-thumb"><a target="_blank" href="' . get_permalink() . '&section=shows&g=' . $page_id . '">';
						        the_post_thumbnail('medium');
						        echo '</a>';
						        
								$title = get_the_title();
								$segs = explode( "-", $title );
					?>

								<a target="_blank" href="<?php echo get_permalink() . '&section=shows&g=' . $page_id; ?>">
									<div class='title'><?php echo $segs[0]; ?></div>
									<div class='title'><?php echo $segs[1]; ?></div>
								</a>
					<?php
						        echo '<div class="date">' . get_the_date() . '</div>';
						        echo '</div>';
						    endwhile;
						endif;
					?>

				</div>
				<div class='p-gallery-pagination'>
					<?php
						$tax = 'post_tag';
						$term_name = $page_id;
						$term = get_term_by('name', $term_name, $tax);
						$post_count = $term->count;
						$page_count = ceil($post_count / $posts_per_page);						
						// echo $post_count;
						if ($page_count >= $page_number) {
							$page_min = ($page_number > 5) ? ($page_number-5) : 1;
							$page_max = ($page_count - $page_min > 7) ? ($page_min+7) : $page_count;

							if ($page_number > 1)
								echo "<a class='flip' href='gallery.php?section=shows&g=" . $page_id . "&p=" . ($page_number-1) . "'><</a>";
							for ($i=$page_min; $i<=$page_max; $i++) {
								$class = ($i === $page_number) ? 'class="active"' : '';
								echo "<a " . $class . "href='gallery.php?section=shows&g=" . $page_id . "&p=" . $i . "'>" . $i . "</a>";
							}
								
							if ($page_count > $page_max)
								echo "<span>...</span><a href='gallery.php?section=shows&g=" . $page_id . "&p=" . $page_count . "'</a>";
							if ($page_number < $page_count)
								echo "<a class='flip' href='gallery.php?section=shows&g=" . $page_id . "&p=" . ($page_number+1) . "'>></a>";
						}
						// else 
							// echo 'no pagination count=' . $page_count . '; max=' . $page_max . '; min=' . $page_min . '; num=' . $page_number;
						
					?>
				</div>
			</div>
		</div>
		<?php include 'wp-content/themes/zhuli/service-footer.php'; ?>
	</body>